<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: showranking.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
  
  /**
   * diplays the ranking
   *
   * @param string $jl_func value of $func
   * @param int $r round (matchcode) to display 
   * @param array $overall_config
   */
  function showRanking ($jl_func, $r, &$overall_config) 
  {
    global $joomleague, $database;
    global $htmlHelper;
    
    if ($joomleague->project_type == 'SIMPLE_KO') 
    {
      $url = sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showResults&amp;p='.$joomleague->id.JL_ITEMID_SUFFIX);
      mosRedirect($url);
    }
    // url if no sef link comes along (ranking form)
    $url            = JL_COMPONENT_LINK.'&amp;func=showRanking&amp;p='.$joomleague->id;
    $table_config   = $joomleague->getTemplateConfig("show_table.tpl");
    $part           = intval(mosGetParam($_REQUEST, 'part', 0));
    $round          = ($r == 0) ? $joomleague->current_round : $r;
    
    $rounds = $joomleague->getRoundNames();
    if ($part==1) {
    	$from = 1;
    	$to = intval(count($rounds)/2);
    }
    elseif ($part==2) {
    	$from = intval(count($rounds)/2)+1;
    	$to = count($rounds);
    }
    else {
    	$from = intval(mosGetParam($_REQUEST, 'from', 1));
    	$to = intval(mosGetParam($_REQUEST, 'to', $round));
    }
    if($part > 0 ) $url.='&amp;part='.$part;
    elseif ($from != 1 || $to != $round) $url.='&amp;from='.$from.'&amp;to='.$to;
    $type = intval(mosGetParam($_REQUEST, 'type', 0));
    if ($type > 0) $url.='&amp;type='.$type;
    if ($type == 3) 
    {
      $reqtype = 0;
    }
    else
    {
      $reqtype = $type;
    }
    
    $divLevel=0;
    //for sub division ranking tables
    if ($joomleague->project_type=='DIVISIONS_LEAGUE') {
    
    	$selDivision = intval(mosGetParam($_REQUEST, 'division', 0));
    	$divLevel=intval(mosGetParam($_REQUEST, 'divLevel', $table_config['default_division_view']));
    	
    	if ($selDivision > 0) {
    	  $url .= '&amp;division='.$selDivision;
    	  $divisions = array($selDivision);
    	}
    	else {
    		// check if division level view is allowed. if not, replace with default
	      if (    ( $divLevel == 0 && $table_config['show_project_table']==0 )
	           || ( $divLevel == 1 && $table_config['show_level1_table']== 0 )
	           || ( $divLevel == 2 && $table_config['show_level2_table']==0 ) ) {
	        $divLevel = $table_config['default_division_view'];
	      }
	      $url .= '&amp;divLevel='.$divLevel;
	      if ( $divLevel ) $divisions = $joomleague->getDivisionsId( $divLevel );
	      else $divisions = array(0); //project
    	}
    } 
    else $divisions = array(0); //project
    $selectedvalue=0;

    $last = intval(mosGetParam($_REQUEST, 'last', 0));
    if ($last > 0) $url .= '&amp;last='.$last;
    if (mosGetParam($_REQUEST, 'sef', 0) == 1) mosRedirect(sefRelToAbs($url.JL_ITEMID_SUFFIX));
    
    //create ranking object
    $ranking = new Ranking();
    $ranking->setProjectConfig($joomleague, $table_config['rankingorder']);
    $previous_ranking=array();
    
    foreach ($divisions as $division) {
      $current_ranking[$division] = $ranking->getRanking($reqtype, $to, $from, $table_config['show_negative_points'], $table_config['sort_table'], $division);
      if( $table_config['last_ranking']==1 )
      {      
        if ( $to == 1 || ( $to == $from ) ) {//no previous ranking...
        	$previous_ranking[$division] = &$current_ranking[$division];
        }
      	else $previous_ranking[$division] = $ranking->getRanking($reqtype, $to - 1, $from, $table_config['show_negative_points'], $table_config['sort_table'], $division);
      }
      if ($type == 3) 
      {
        // calculate other rankings for triple table
        $homeRank[$division] = $ranking->getRanking(1, $to, $from, $table_config['show_negative_points'], $table_config['sort_table']);
        $awayRank[$division] = $ranking->getRanking(2, $to, $from, $table_config['show_negative_points'], $table_config['sort_table']);
      }
    }
    
    $colors = $joomleague->getColors($table_config['colors']);
    $query = "SELECT info FROM #__joomleague_team_joomleague WHERE project_id = ".$joomleague->id;
    $database->setQuery($query);
    $result = $database->loadObjectList();
    
    //$rankingnav=$ranking->navRanking($joomleague->id, count($rounds), $round, $roundbegin);
    $pageNav=$joomleague->pagenav($jl_func,count($rounds), $to);
    $pageNav2=$joomleague->pagenav2($jl_func, count($rounds), $to);
	  $current_round=$round;

    require(getTemplateFile(JL_TEMPLATE_PATH, 'show_table.tpl.php'));
    
// Tabelle Ende
//##########################################################################################################################
  }
?>
